﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using Newtonsoft.Json.Linq;

namespace NetCoreCMS.Web.Areas.Admin.Controllers.Article
{
    public class Comment_ListController : ManageController
    {
        protected const string WEB_VIEW = "~/Areas/Admin/Views/Article/comment_list.cshtml";
        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected int channel_id;
        protected string property = string.Empty;
        protected string keywords = string.Empty;
        public IArticle_commentService _article_comment { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            this.channel_id = CMSRequest.GetQueryInt("channel_id", filterContext.HttpContext);
            ChkAdminLevel("article_" + this.channel_id + "_comment", CMSEnums.ActionEnum.View.ToString()); //检查权限
            ActionResult result = View(EDIT_RESULT_VIEW);
            
            this.property = Request.Query["property"];
            this.keywords = Request.Query["keywords"];
            if (int.TryParse(Request.Query["pagesize"], out this.pageSize))
            {
                if (this.pageSize > 0)
                {
                    Response.Cookies.Append("article_comment_page_size", this.pageSize.ToString());
                }
            }
            else
            {
                this.pageSize = GetPageSize(10); //每页数量
            }
            ViewBag.ChannelId = this.channel_id;
            ViewBag.Property = this.property;
            ViewBag.Keywords = this.keywords;
            ViewBag.PageSize = this.pageSize.ToString();
        }
        //
        // GET: /admin/Comment_List/

        public ActionResult Index()
        {
            RptBind("where channel_id = " + this.channel_id + CombSqlTxt(this.keywords, this.property), "add_time desc");
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitAudit()
        {
            ChkAdminLevel("article_" + this.channel_id + "_comment", CMSEnums.ActionEnum.Audit.ToString()); //检查权限
            JObject jobject = JObject.Parse(Request.Form["json"]);
            JToken list = jobject["list"];
            foreach (JToken item in list)
            {
                int id = int.Parse(item["id"].ToString());
                _article_comment.UpdateField(id, "is_lock=0");
            }
            AddAdminLog(CMSEnums.ActionEnum.Audit.ToString(), "审核评论信息"); //记录日志
            string script = JscriptMsg("审核通过成功！", Utils.CombUrlTxt("index", "channel_id={0}&keywords={1}&property={2}",
                this.channel_id.ToString(), this.keywords, this.property));
            return Content(script);
        }

        [HttpPost]
        public ActionResult SubmitDelete()
        {
            ChkAdminLevel("article_" + this.channel_id + "_comment", CMSEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            JObject jobject = JObject.Parse(Request.Form["json"]);
            JToken list = jobject["list"];
            foreach (JToken item in list)
            {
                int id = int.Parse(item["id"].ToString());
                if (_article_comment.Delete(id))
                {
                    sucCount += 1;
                }
                else
                {
                    errorCount += 1;
                }
            }
            AddAdminLog(CMSEnums.ActionEnum.Delete.ToString(), "删除评论成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            string script = JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！",
                Utils.CombUrlTxt("index", "channel_id={0}&keywords={1}&property={2}", this.channel_id.ToString(), this.keywords, this.property));
            return Content(script);
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            int.TryParse(Request.Query["page"], out this.page);
            this.page = this.page <= 0 ? 1 : this.page;
            ViewData["list"] = _article_comment.GetList(this.page, this.pageSize, _strWhere, _orderby, null);
            this.totalCount = _article_comment.RecordCount(_strWhere);
            string pageUrl = Utils.CombUrlTxt("index", "channel_id={0}&keywords={1}&property={2}&page={3}",
                this.channel_id.ToString(), this.keywords, this.property, "__id__");
            ViewBag.PageContent = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords, string _property)
        {
            StringBuilder strTemp = new StringBuilder();           
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and (user_name like '%" + _keywords + "%' or content like '%" + _keywords + "%')");
            }
            if (!string.IsNullOrEmpty(_property))
            {
                switch (_property)
                {
                    case "isLock":
                        strTemp.Append(" and is_lock=1");
                        break;
                    case "unLock":
                        strTemp.Append(" and is_lock=0");
                        break;
                }
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回评论每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Request.Cookies["article_comment_page_size"], out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion
    }
}
